package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.service.AgencyCertificationAuditService;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;

/**
* @author 22096
* @description 针对表【agency_certification_audit(机构认证审核表)】的数据库操作Service实现
* @createDate 2024-12-03 11:15:07
*/
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit>
    implements AgencyCertificationAuditService{

    /**
     * 审核机构认证分页查询
     * @return
     */
    @Override
    public PageResult<AgencyCertificationAuditResDTO> pageQuery(AgencyCertificationAuditPageQueryReqDTO dto) {
        Page<AgencyCertificationAudit> page = PageUtils.parsePageQuery(dto, AgencyCertificationAudit.class);
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper = Wrappers.<AgencyCertificationAudit>lambdaQuery().like(ObjectUtil.isNotEmpty(dto.getName()),AgencyCertificationAudit::getName, dto.getName())
                .eq(ObjectUtil.isNotEmpty(dto.getAuditStatus()), AgencyCertificationAudit::getAuditStatus, dto.getAuditStatus())
                .eq(ObjectUtil.isNotEmpty(dto.getCertificationStatus()), AgencyCertificationAudit::getCertificationStatus, dto.getCertificationStatus())
                .like(ObjectUtil.isNotEmpty(dto.getLegalPersonName()),AgencyCertificationAudit::getLegalPersonName,dto.getLegalPersonName());
        Page<AgencyCertificationAudit> page1 = this.page(page, queryWrapper);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page1.getTotal());
        pageResult.setList(page1.getRecords());
        pageResult.setPages(page1.getPages());
        return pageResult;
    }

    /**
     * 审核机构认证信息
     * @param id
     * @param certificationStatus
     * @param rejectReason
     */
    @Override
    public void audit(Long id, Integer certificationStatus, String rejectReason) {
        AgencyCertificationAudit agencyCertificationAudit = this.getById(id);
        if (ObjectUtil.isEmpty(agencyCertificationAudit)){
            throw new CommonException("当前机构不存在");
        }
        LambdaUpdateWrapper<AgencyCertificationAudit> updateWrapper = Wrappers.<AgencyCertificationAudit>lambdaUpdate().set(AgencyCertificationAudit::getCertificationStatus, certificationStatus).set(AgencyCertificationAudit::getAuditStatus, CertificationStatusEnum.PROGRESSING.getStatus()).set(ObjectUtil.isNotEmpty(rejectReason), AgencyCertificationAudit::getRejectReason, rejectReason).eq(AgencyCertificationAudit::getId, id);
        boolean update = this.update(updateWrapper);
        if (!update){
            throw new DBException("操作失败");
        }
    }


}




